//Shkolnij Rostislav
package CalculationCore;

import java.util.Vector;

public class CCPUMemory {
	private Vector<CCPURegister> vecM;
	private static final int iModificationTable[] = 
	{-1,-1,-1,-1, +1,+1,+1,0, 0,0,0,0, 0,0,0,0};
	@SuppressWarnings("unused")
	private boolean IsRealMode;

	public CCPUMemory(int num)
	{
		vecM = new Vector<CCPURegister>(num);
		for(int i=0; i<num; i++)
		{
			vecM.add(new CCPURegister());
		}
		
		if(num == 14)
			IsRealMode = true;
		else
			IsRealMode = false;
	}
	
	public CCPUMemory(double[] src)
	{
		int num = src.length;
		
		vecM = new Vector<CCPURegister>(num);
		for(int i=0; i<num; i++)
		{
			vecM.add(new CCPURegister(src[i]));
		}
		
		if(num == 14)
			IsRealMode = true;
		else
			IsRealMode = false;
	}
	
	public int getSize()
	{
		return vecM.size();
	}
	
	public CCPURegister getMbyIndex(int index) {
		if(index >= 0 &&  index < vecM.size())
			return vecM.get(index);
		else
		{
			System.out.println(String.format("!!! Index %d is out of range", index));
			return null;
		}
	}
	
	public double[] getDoubleArray() {
		double[] res = new double[vecM.size()]; 
		
		for(int i=0; i<vecM.size(); i++)
			res[i] = vecM.get(i).getdFullNumber();
		return res;
	}
	
	public void setDoubleArray(double[] src) {
		if(src.length != vecM.size()){
			System.out.println(String.format(" !!! Src length must be = %d", this.vecM.size()));
		} else {
			for(int i=0; i<vecM.size(); i++)
				vecM.get(i).setdFullNumber(src[i]);
		}
	}
	
	public CCPURegister getMbyIndexWithDecrement(int index) {
		if(index >= 1 &&  index < vecM.size()) {
			vecM.set(index, 
					new CCPURegister(vecM.get(index).getdFullNumber() - 1));
			return vecM.get(index);
		} else {
			System.out.println(String.format("!!! Index %d is out of range", index));
			return null;
		}
	}
	
	public int getModifiedAddressbyIndex(int index) {
		if(index >= 0 &&  index < vecM.size()) {
			
			int new_adr = vecM.get(index).getAddress() + iModificationTable[index];
			vecM.set(index, new CCPURegister(new_adr));
			return vecM.get(index).getAddress();
		}
		else
		{
			System.out.println(String.format("!!! Index %d is out of range", index));
			return 0;
		}
	}

	public void setMbyIndex(int index, CCPURegister src) {
		if(index >= 0 &&  index < vecM.size())
			vecM.set(index, src);
		else
			System.out.println(String.format("!!! Index %d is out of range", index));
	}

	public void systemOutput(char c)
	{
		System.out.println(String.format(" Memmory :"));
		for(int i=0; i<vecM.size(); i++)
		{
			System.out.print(String.format("%c%H  :", c, i));
			vecM.get(i).systemOutput();
		}
	}
	
	public void cleanup()	{
		
		for(int i=0; i<vecM.size(); i++)
			vecM.get(i).cleanup();
	}
	
	public boolean compareAllRegisters(double[] src, double rounding)
	{
		boolean res = true;
		if(src.length == this.vecM.size())	{
			
			for(int i = 0; i<src.length && res; i++) {
				res = this.vecM.get(i).isTheSameValue(src[i], rounding);
			}
			return res; 
		} else {
			System.out.println(String.format(" !!! Src length must be = %d", this.vecM.size()));
			return false; 
		}
	}
}
